package org.eiahe.hr.attendance.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;
import org.ehe.common.core.BaseController;
import org.ehe.common.core.domain.R;
import org.ehe.common.core.validate.AddGroup;
import org.ehe.common.core.validate.EditGroup;
import org.ehe.common.excel.utils.ExcelUtil;
import org.ehe.common.log.annotation.Log;
import org.ehe.common.log.enums.BusinessType;
import org.ehe.common.mybatis.core.page.PageQuery;
import org.ehe.common.mybatis.core.page.TableDataInfo;
import org.eiahe.hr.attendance.domain.bo.AttendanceAlertBo;
import org.eiahe.hr.attendance.domain.vo.AttendanceAlertVo;
import org.eiahe.hr.attendance.service.IAttendanceAlertService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import jakarta.servlet.http.HttpServletResponse;
import java.time.LocalDate;
import java.util.List;

/**
 * 考勤异常提醒Controller
 *
 * @author zhangnn
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/attendance/alert")
public class AttendanceAlertController extends BaseController {

    private final IAttendanceAlertService attendanceAlertService;

    /**
     * 查询考勤异常提醒列表
     */
    @SaCheckPermission("attendance:alert:list")
    @GetMapping("/list")
    public TableDataInfo<AttendanceAlertVo> list(AttendanceAlertBo bo, PageQuery pageQuery) {
        return attendanceAlertService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出考勤异常提醒列表
     */
    @SaCheckPermission("attendance:alert:export")
    @Log(title = "考勤异常提醒", linkType = "attendance", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(AttendanceAlertBo bo, HttpServletResponse response) {
        List<AttendanceAlertVo> list = attendanceAlertService.queryList(bo);
        ExcelUtil.exportExcel(list, "考勤异常提醒", AttendanceAlertVo.class, response);
    }

    /**
     * 获取考勤异常提醒详细信息
     */
    @SaCheckPermission("attendance:alert:query")
    @GetMapping("/{alertId}")
    public R<AttendanceAlertVo> getInfo(@PathVariable Long alertId) {
        return R.ok(attendanceAlertService.queryById(alertId));
    }

    /**
     * 新增考勤异常提醒
     */
    @SaCheckPermission("attendance:alert:add")
    @Log(title = "考勤异常提醒", linkType = "attendance", businessType = BusinessType.INSERT)
    @PostMapping
    public R<Void> add(@Validated(AddGroup.class) @RequestBody AttendanceAlertBo bo) {
        return toAjax(attendanceAlertService.insertByBo(bo));
    }

    /**
     * 修改考勤异常提醒
     */
    @SaCheckPermission("attendance:alert:edit")
    @Log(title = "考勤异常提醒", linkType = "attendance", businessType = BusinessType.UPDATE)
    @PutMapping
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody AttendanceAlertBo bo) {
        return toAjax(attendanceAlertService.updateByBo(bo));
    }

    /**
     * 删除考勤异常提醒
     */
    @SaCheckPermission("attendance:alert:remove")
    @Log(title = "考勤异常提醒", linkType = "attendance", businessType = BusinessType.DELETE)
    @DeleteMapping("/{alertId}")
    public R<Void> remove(@PathVariable Long alertId) {
        return toAjax(attendanceAlertService.deleteWithValidById(alertId));
    }

    /**
     * 生成指定日期的考勤异常提醒
     */
    @SaCheckPermission("attendance:alert:generate")
    @Log(title = "生成考勤异常提醒", linkType = "attendance", businessType = BusinessType.INSERT)
    @PostMapping("/generate")
    public R<Void> generate(@RequestParam LocalDate alertDate) {
        attendanceAlertService.generateAlerts(alertDate);
        return R.ok();
    }

    /**
     * 发送月度异常汇总邮件
     */
    @SaCheckPermission("attendance:alert:sendSummary")
    @Log(title = "发送月度异常汇总邮件", linkType = "attendance", businessType = BusinessType.OTHER)
    @PostMapping("/sendSummary")
    public R<Void> sendMonthlySummary(@RequestParam Integer year, @RequestParam Integer month) {
        attendanceAlertService.sendMonthlyAlertSummary(year, month);
        return R.ok();
    }


}
